package com.pets.springboot_petshop.mapper;

import com.pets.springboot_petshop.entity.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface UserMapper {
    //根据用户id查询用户
    @Select("select  a.id,a.username,a.password,a.email,a.phone,a.img,a.membership_id,b.name,b.discount,a.integral,a.create_time,a.update_time " +
            "from user a LEFT JOIN membershiptype b on a.membership_id = b.id where a.id = #{id};")
    User select(int id);
    //根据用户名查询用户
    @Select("select  a.id,a.username,a.password,a.email,a.phone,a.img,a.membership_id,b.name,b.discount,a.integral,a.create_time,a.update_time " +
            "from user a LEFT JOIN membershiptype b on a.membership_id = b.id where a.username = #{username};")
    User findByUserName(String username);
    //根据用户邮箱查询用户
    @Select("select  a.id,a.username,a.password,a.email,a.phone,a.img,a.membership_id,b.name,b.discount,a.integral,a.create_time,a.update_time " +
            "from user a LEFT JOIN membershiptype b on a.membership_id = b.id where a.email = #{email};")
    User findByUseremail(String email);
    //根据用户名模糊查询用户
    @Select("select a.id,a.username,a.email,a.phone,a.img,a.membership_id,b.name,a.integral,a.create_time,a.update_time " +
            "from user a LEFT JOIN membershiptype b on a.membership_id = b.id where a.username like concat('%',#{username},'%');")
    List<User> selectName(String username);
    //根据用户邮箱模糊查询用户
    @Select("select a.id,a.username,a.email,a.phone,a.img,a.membership_id,b.name,a.integral,a.create_time,a.update_time " +
            "from user a LEFT JOIN membershiptype b on a.membership_id = b.id where a.email like concat('%',#{email},'%');")
    List<User> selectemail(String email);
    @Select("SELECT id FROM user WHERE username = #{username}")
    int selectusername(String username);
    //管理员添加用户
    @Insert("insert into user(username,phone,email,integral,membership_id,create_time,update_time)values(#{username},#{phone},#{email},0,1,now(),now());")
    void addadmin(String username, String phone, String email);
    //用户添加用户
    @Insert("insert into user(username,password,email,integral,membership_id,create_time,update_time)values(#{username},#{password},#{email},0,1,now(),now());")
    void addUser(String username, String password, String email);
    //修改用户密码
    @Update("UPDATE user SET password=#{password},update_time=now() WHERE email = #{email} ;")
    int updateUserpassword(User user);
    //管理员修改用户数据
    @Update("UPDATE user SET username=#{username},email=#{email},img=#{img},phone=#{phone},integral=#{integral},membership_id=#{membership_id},update_time=now() where id= #{id};")
    int updateUserById(User user);
    //用户修改用户数据
    @Update("UPDATE user SET username=#{username},img=#{img},email=#{email},phone=#{phone},update_time=now() where id= #{id};")
    int updateUser(User user);
    //删除
    @Delete("delete from user where id=#{id};")
    int deleteUserById(int id);
    //查询所有
    @Select("select a.id,a.username,a.email,a.phone,a.img,a.membership_id,a.integral,b.name,a.create_time,a.update_time from user a LEFT JOIN membershiptype b on a.membership_id = b.id;")
    List<User> UserfindAll();
    //修改用户积分
    @Update("UPDATE user SET integral=#{integral},update_time=now() WHERE username = #{username} ;")
    int updateintegral(User user);
    //修改用户会员等级
    @Update("UPDATE user SET membership_id=#{membership_id},update_time=now() WHERE username = #{username} ;")
    int updatemembership(User user);
    //修改用户头像
    @Update("UPDATE user SET img=#{img},update_time=now() WHERE id = #{id} ;")
    int updateimg(User user);
    //修改用户名
    @Update("UPDATE user SET username=#{username},update_time=now() WHERE id = #{id} ;")
    int updateusername(User user);
    //修改用户手机号
    @Update("UPDATE user SET phone=#{phone},update_time=now() WHERE id = #{id} ;")
    int updatephone(User user);
    //修改用户手机号
    @Update("UPDATE user SET email=#{email},update_time=now() WHERE id = #{id} ;")
    int updatemail(User user);
    // 批量删除
    @Delete({
            "<script>",
            "DELETE FROM user WHERE id IN",
            "<foreach item='item' collection='array' open='(' separator=',' close=')'>",
            "#{item}",
            "</foreach>",
            "</script>"
    })
    int admindelete(@Param("array") int[] ids);
}

